package com.mxy.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mxy.myapplication.R;
import com.mxy.myapplication.activity.MainActivity;
import com.mxy.myapplication.adapter.AdapterForRv_Grid;
import com.mxy.myapplication.util.LinearLayoutColorDivider;
import com.mxy.myapplication.util.itemtouchhelperUtil.SimpleItemTouchHelperCallback;

/**
 * ViewPager上的碎片 网格布局
 */

public class FragmentOfViewPager_Grid extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment, container, false);
        RecyclerView rv = (RecyclerView) v.findViewById(R.id.rv);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);

        rv.setLayoutManager(gridLayoutManager);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new LinearLayoutColorDivider(getActivity().getResources(),
                android.R.color.transparent, R.dimen.dip0, LinearLayoutManager.VERTICAL));
        MainActivity mainActivity = (MainActivity) this.getActivity();
        AdapterForRv_Grid adapter = new AdapterForRv_Grid(mainActivity, gridLayoutManager,
                mainActivity.swipe);

        //ItemTouchHelper功能实现
        SimpleItemTouchHelperCallback simpleItemTouchHelperCallback =
                new SimpleItemTouchHelperCallback(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchHelperCallback);
        itemTouchHelper.attachToRecyclerView(rv);

        rv.setAdapter(adapter);

        return v;
    }
}